package org.laowenchao.dao;

import java.util.List;

import javax.jdo.PersistenceManager;

import org.laowenchao.server.PMF;






public class BaseDao {


	public <T extends Object> boolean addObject(T o){
		boolean b = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(o);
			b = true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		finally
		{
			pm.close();
		}
		return b;

	}
	
	
	
	/**
	 * need the Object is detachable and setDetachAllOnCommit(true) when getById
	 * */
	public <T extends Object> boolean updateObject(T o){
		boolean b = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(o);
			b = true;
		}catch(Exception ex){
			ex.printStackTrace();
		}finally
		{
			pm.close();
		}
		return b;
	}
	
	public <T extends Object> List<T> getObjectList(Class<T> clazz)
	{
		List list = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from "+clazz.getName();
			list = (List)pm.newQuery(query).execute();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			pm.close();
		}
		
		return list;
		
	}
	
}
